package com.bdcom.autotest.telnet.util.ping;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ping {
    /**
     * 调用CMD去ping 一个地址 并返回结果
     * @param ip
     * @return
     * @throws IOException
     */
    public static boolean ping(String ip) throws Exception{
        Process p = null;
        BufferedReader buf=null;
        boolean isIpV6=false;
        if(ip.contains(":")) {
            isIpV6=true;
        }

        try {
            if(OSinfo.isWindows()) {
                if(!isIpV6) {
                    p = Runtime.getRuntime().exec("ping " +ip +" -n 4");
                }
                if(isIpV6) {
                    p = Runtime.getRuntime().exec("ping -6 " +ip +" -n 4");
                    ip= IPV6Util.parseFullIPv6ToAbbreviation(ip);
                }

            }

            if(OSinfo.isMacOSX()) {
                if(!isIpV6) {
                    p = Runtime.getRuntime().exec("ping " +ip+" -c 4");
                }
                if(isIpV6) {
                    p = Runtime.getRuntime().exec("ping6 " +ip+" -c 4");
                    ip= IPV6Util.parseFullIPv6ToAbbreviation(ip);
                }

            }
            if(OSinfo.isLinux()) {
                if(!isIpV6) {
                    p = Runtime.getRuntime().exec("ping " +ip+" -c 4");
                }
                if(isIpV6) {
                    p = Runtime.getRuntime().exec("ping6 " +ip+" -c 4");
                    ip=IPV6Util.parseFullIPv6ToAbbreviation(ip);
                }

            }

            buf = new BufferedReader(new InputStreamReader(p.getInputStream()  , OSinfo.getSysEncoding() )    );
            String line = "";
            while ((line = buf.readLine()) != null) {
                if((line.toLowerCase().indexOf("from")!=-1||line.indexOf("来自")!=-1) && line.indexOf(ip)!=-1){
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            throw e;
        } finally{
            buf.close();
        }
    }
    
    
    public static boolean ping2(String ip) throws Exception{
        Process p = null;
        BufferedReader buf=null;
        boolean isIpV6=false;
        if(ip.contains(":")) {
            isIpV6=true;
        }

        try {
            if(OSinfo.isWindows()) {
                if(!isIpV6) {
                    p = Runtime.getRuntime().exec("ping " +ip +" -n 2");
                }
                if(isIpV6) {
                    p = Runtime.getRuntime().exec("ping -6 " +ip +" -n 2");
                    ip= IPV6Util.parseFullIPv6ToAbbreviation(ip);
                }

            }

            if(OSinfo.isMacOSX()) {
                if(!isIpV6) {
                    p = Runtime.getRuntime().exec("ping " +ip+" -c 2");
                }
                if(isIpV6) {
                    p = Runtime.getRuntime().exec("ping6 " +ip+" -c 2");
                    ip= IPV6Util.parseFullIPv6ToAbbreviation(ip);
                }

            }
            if(OSinfo.isLinux()) {
                if(!isIpV6) {
                    p = Runtime.getRuntime().exec("ping " +ip+" -c 2");
                }
                if(isIpV6) {
                    p = Runtime.getRuntime().exec("ping6 " +ip+" -c 2");
                    ip=IPV6Util.parseFullIPv6ToAbbreviation(ip);
                }

            }

            buf = new BufferedReader(new InputStreamReader(p.getInputStream()  , OSinfo.getSysEncoding() )    );
            String line = "";
            while ((line = buf.readLine()) != null) {
                if((line.toLowerCase().indexOf("from")!=-1||line.indexOf("来自")!=-1) && line.indexOf(ip)!=-1){
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            throw e;
        } finally{
            buf.close();
        }
    }
    

    public static void main(String[] args) throws Exception {
        String ip=args[0];
        System.out.println(ip+" ping result:"+ping(ip));
    }
}
